package com.diablo.sitemanager.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by diablo13064 on 2018/3/26.
 */


public class FileUtil {
    Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 写文件
     * @param is
     * @param os
     * @throws IOException
     */
    public static void writeFile(InputStream is, OutputStream os) throws IOException {
        FileCopyUtils.copy(is, os);
    }

    /**
     * 删除文件夹下所有文件
     * @param file
     */
    public static void DeleteAllFile(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    f.delete();
                }
            } else if (file.isFile()) {
                file.delete();
            }
        }
    }

    /**
     * 删除文件夹些所有指定后缀的文件
     * @param file
     * @param filter
     */
    public static void DeleteAllFile(File file,String filter) {
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    if (f.getName().endsWith(filter) || f.getName().endsWith(filter.toUpperCase())) {
                        f.delete();
                    }
                }
            } else if (file.isFile()) {
                if (file.getName().endsWith(filter) || file.getName().endsWith(filter.toUpperCase()) ) {
                    file.delete();
                }
            }
        }
    }

    public static File MultipartFile2File(MultipartFile file) {
        //return file.t
        return new File("");
    }
    /**
     * 文件到Byte
     * @param filePath
     * @return
     */
    public static byte[] File2byte(String filePath)
    {
        byte[] buffer = null;
        try
        {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * Byte到文件
     * @param buf
     * @param filePath
     * @param fileName
     */
    public static void byte2File(byte[] buf, String filePath, String fileName)
    {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try
        {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory())
            {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (bos != null)
            {
                try
                {
                    bos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 时间转字符串
     * @param date
     * @param format
     * @return
     */
    public static String Date2String(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 字符串转时间
     * @param s
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date String2Date(String s, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(s);
    }

    /**
     * 判断是否存在汉字
     * @param str
     * @return
     */
    public static boolean isChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find())
            flg = true;
        return flg;
    }

    public static String makeFileName(Object object) {
        String fileName = "";
        String suffix = "";
        String fullName = "";
        if (object instanceof MultipartFile) {
             fullName =  ((MultipartFile) object).getOriginalFilename();
        }
        else if(object instanceof File){
             fullName =  ((File) object).getName();
        } else if (object instanceof String) {
             fullName = (String) object;
        }
        suffix = fullName.substring(fullName.indexOf("."));
        String s=new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
        fileName = s + suffix;

        return  fileName;
    }


}
